*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear

*******************************************************************************
* Label candidates coded by others as progressive
*******************************************************************************

gen label = 0 // ADH
replace label = 1 if regex(lower(candidate), "salaz")
replace label = 1 if regex(lower(candidate), "becton")
replace label = 1 if regex(lower(candidate), "krasner")
replace label = 1 if regex(lower(candidate), "dehghani")
replace label = 1 if regex(lower(candidate), "descano")
replace label = 1 if regex(lower(candidate), "gascon")
replace label = 1 if regex(lower(candidate), "rocah")
gen labeltext = "ADH Coding" if ideology <= -.6 & ///
	regex(lower(candidate), "bec")

*******************************************************************************
* Drop if missing
*******************************************************************************

drop if mi(ideology)

*******************************************************************************
* Panel A: Graph candidate ideology against Trump vote
*******************************************************************************

cor ideology trumpvoteshare
local myr = string(r(rho), "%20.02fc")
cor ideology trumpvoteshare [aw=population]
local myrw = string(r(rho), "%20.02fc")

#delimit;

gr tw
	(sc ideology trumpvoteshare if label == 0, col(black) msym(O))
	(sc ideology trumpvoteshare if label == 1, 
		msym(O) col(gray))
	(sc ideology trumpvoteshare if !mi(labeltext), 
		mlab(labeltext) msym(none) mlabpos(3) mlabcol(gray))
	(lfit ideology trumpvoteshare, col(black) lwid(medthick))
 	,	
	legend(off)
	plotregion(style(none))
	xtitle("Trump Vote Share")
	ytitle("Candidate Conservatism")
	ylab(, angle(horiz))	
	title("(a) Candidate and Voter Conservatism, Candidate-Level")
	name(g1, replace)
	text(-.95 0.8 "r=`myr'", col(gray))
	;
	
#delimit cr

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure2_a.pdf", replace

*******************************************************************************
* Panel B: Graph average candidate ideology against Trump vote by district
*******************************************************************************

preserve

	collapse ideology trumpvoteshare population label, by(district)

	replace label = 1 if label > 0

	cor ideo trumpvoteshare
	local myr = string(r(rho), "%20.02fc")
	cor ideo trumpvoteshare [aw=pop]
	local myrw = string(r(rho), "%20.02fc")

	#delimit;

	gr tw
		(sc ideology trumpvoteshare, col(black) msym(O))
		(lfit ideology trumpvoteshare, col(black) lwid(medthick))
		,	
			plotregion(style(none))
			xtitle("Trump Vote Share")
			ytitle("Candidate Conservatism")
			ylab(, angle(horiz))
			legend(off)
			title(
			 "(b) Candidate and Voter Conservatism, District-Level"
			)
			name(g2, replace)
			text(-.46 0.8 "r=`myr'", col(gray))
			;

	#delimit cr

restore

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure2_b.pdf", replace

*******************************************************************************
* Panel C: Police donations against ideology
*******************************************************************************

gen lamount = log(amount*100 + 1)
gen ldonors = log(donors*100 + 1)

cor ideology lamount

local myr = string(r(rho), "%20.02fc")

foreach p in 0 1 5 10 20 30 {
	local lp = log(`p' + 1)
	local p = string(`p', "%20.0fc")
	if length("`p'") == 1 local pp ".0`p'"
	if length("`p'") == 2 local pp ".`p'"
	if "`pp'" == ".00" local pp "0"
	local myxlab `myxlab' `lp' "`pp'"
}

#delimit;

gr tw
	(sc ideology lamount if label == 0, col(black) msym(O))
	(sc ideology lamount if label == 1, 
		msym(O) col(gray))
	(lfit ideology lamount , col(black) lwid(medthick))
	,
		ylab(, angle(horiz))
		xlab(`myxlab')
		legend(off)
		plotregion(style(none))
		ytitle("Candidate Conservatism")
		xtitle("Share Donations from Law Enforcement")
		text(-1 3.4 "r=`myr'", col(gray))
		name(g3, replace)
		title("(c) Candidate Conservatism and Police Donations ")
		;

#delimit cr

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure2_c.pdf", replace

*******************************************************************************
* Panel D: Ideology by party
*******************************************************************************

collapse ideology (semean) se=ideology, by(party)
encode party, gen(partyc)
gen ub = ideology + 1.96*se
gen lb = ideology - 1.96*se

#delimit;

gr tw
	(sc ideology partyc, col(black))
	(rspike ub lb partyc, col(black))
	,
		legend(off)
		plotregion(style(none))
		ylab(, angle(horiz))
		ytitle("Candidate Conservatism")
		xtitle("")
		xlab(1 "Democrat" 2 "Independent" 3 "Republican")
		xscale(range(.75 3.25))
		name(g4, replace)
		;

#delimit cr

*******************************************************************************
* Output
*******************************************************************************

gr export "_output/figure2_d.pdf", replace

*******************************************************************************
* End
*******************************************************************************
	
